package com.ihr360.oneclick.controller;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.oneclick.entity.OneClickTerm;
import com.ihr360.oneclick.request.ModifyTermEnableRequest;
import com.ihr360.oneclick.request.SearchRequest;
import com.ihr360.oneclick.service.OneClickTermService;
import com.ihr360.oneclick.vo.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tom
 * @date 17-12-19
 */
@RestController
@RequestMapping("/term")
@Api(value = "/term", description = "一键功能设置接口相关信息", produces = ("application/json;charset=UTF-8"))
public class OneClickTermController {

    @Autowired
    private OneClickTermService oneClickTermService;

    @PostMapping(value = "/pageData")
    @ApiOperation(value = "/pageData", httpMethod = "POST", notes = "获取一键功能分页数据",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<PageData<OneClickTerm>> getPageData(@RequestBody SearchRequest searchRequest, AuthUserInfo authUserInfo) {
        if (searchRequest.getSortField() == null) {
            searchRequest.setSortDirection(Sort.Direction.ASC);
            searchRequest.setSortField("companyId");
        }
        PageData<OneClickTerm> pageData = oneClickTermService.getPageData(authUserInfo.getCompanyId(), searchRequest);
        return new ResultInfo.Builder<PageData<OneClickTerm>>().success(pageData);
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "/save", httpMethod = "POST", notes = "获取一键功能分页数据",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo save(@RequestBody OneClickTerm oneClickTerm, AuthUserInfo authUserInfo) {
        oneClickTerm.setCompanyId(authUserInfo.getCompanyId());
        oneClickTermService.saveOneClickTerm(oneClickTerm);
        return new ResultInfo.Builder<>().success();
    }

    @PostMapping(value = "/modify/enable")
    @ApiOperation(value = "/modify/enable", httpMethod = "POST", notes = "修改启用状态",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo updateTermEnable(@RequestBody ModifyTermEnableRequest modifyTermEnableRequest) {
        oneClickTermService.modifyOneClickTermEnable(modifyTermEnableRequest.getTermId(), modifyTermEnableRequest.getEnable());
        return new ResultInfo.Builder<>().success();
    }

    @DeleteMapping(value = "/{termId}")
    @ApiOperation(value = "/{termId}", httpMethod = "DELETE", notes = "删除一键功能数据：状态为禁用并且没有发送过消息才可以删除",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo delete(@PathVariable String termId, AuthUserInfo authUserInfo) {
        oneClickTermService.deleteOneClickTerm(authUserInfo.getCompanyId(), termId);
        return new ResultInfo.Builder<>().success();
    }

    @GetMapping(value = "/enableList")
    @ApiOperation(value = "/enableList", httpMethod = "GET", notes = "获取启用状态的所有一键功能列表, 一键功能菜单渲染可调用",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResultInfo<List<OneClickTerm>> getEnableTermList(AuthUserInfo authUserInfo) {
        List<OneClickTerm> oneClickTermList = oneClickTermService.getTermListByEnable(authUserInfo.getCompanyId(), true);
        return new ResultInfo.Builder<List<OneClickTerm>>().success(oneClickTermList);
    }
    
}
